﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Linq;

namespace cndp.Model
{
    /// <summary>
    /// EventJump - Denna klass innehåller information om ett visst hopp som skall göras eller har gjorts
    /// </summary>
    [Serializable]
    public class Eventjump
    {
        #region Constructor

        public Eventjump(int jumpId, string jumpName, string diverId, string diverName, double difficulty, string style, int round,
            double result)
        {
            JumpId = jumpId;
            JumpName = jumpName;
            DiverName = diverName;
            Difficulty = difficulty;
            Style = style;
            Round = round;
            Result = result;
            DiverId = diverId;
        }

        public Eventjump()
        {
            // TODO: Complete member initialization
        }

        #endregion

        #region Variabels/Properties

        [XmlElement("JUMP_ID")]
        public int JumpId { get; set; }

        [XmlElement("JUMP_NAME")]
        public string JumpName { get; set; }

        [XmlElement("DIVER_NAME")]
        public string DiverName { get; set; }

        [XmlElement("DIVER_ID")]
        public string DiverId { get; set; }

        [XmlElement("DIFFICULTY")]
        public double Difficulty { get; set; }

        [XmlElement("STYLE")]
        public string Style { get; set; }

        [XmlElement(typeof (double))] public List<double> JudgePoints = new List<double>();
        [XmlElement("ROUND")]
        public int Round { get; set; }
        [XmlElement("RESULT")]
        public double Result { get; set; }

        #endregion

        #region Methods

        public void AddJudgePoint(double p, Event e)
        {
            if (!(JudgePoints.Count < e.MaxJudges)) return;

            if (p == 0.0 || p == 0.5 || p == 1.0 || p == 1.5 || p == 2.0 || p == 2.5 || p == 3.0 ||
                p == 3.5 || p == 4.0 || p == 4.5 || p == 5.0 || p == 5.5 || p == 6.0 || p == 6.5 ||
                p == 7.0 || p == 7.5 || p == 8.0 || p == 8.5 || p == 9.0 || p == 9.5 || p == 10.0)
            {
                JudgePoints.Add(p);
            }
        }
        public void ChangeJudgePoint(int index,double point)
        {
            JudgePoints[index] = point;
        }

        public void CalculateResult(Event e)
        {
            Difficulty = e.Jumpappendix.FindDifficulty(JumpId, Style, e.JumpHeight);
            double totalpoints = 0;


            List<double> tempJudgePoints = new List<double>(JudgePoints);

            if (JudgePoints.Count >= 5)
            {
                tempJudgePoints.Remove(tempJudgePoints.Max());
                tempJudgePoints.Remove(tempJudgePoints.Min());
            }
            if (JudgePoints.Count >= 7)
            {
                tempJudgePoints.Remove(tempJudgePoints.Max());
                tempJudgePoints.Remove(tempJudgePoints.Min());
            }
            if (JudgePoints.Count >= 9)
            {
                tempJudgePoints.Remove(tempJudgePoints.Max());
                tempJudgePoints.Remove(tempJudgePoints.Min());
            }
            if (JudgePoints.Count >= 11)
            {
                tempJudgePoints.Remove(tempJudgePoints.Max());
                tempJudgePoints.Remove(tempJudgePoints.Min());
            }
            

            for (int i = 0; i < tempJudgePoints.Count; ++i )
            {
                totalpoints += tempJudgePoints[i];
            }

            Result = totalpoints * Difficulty;
        }

        #region Equal override

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            var objAsEventJump = obj as Eventjump;
            if (objAsEventJump == null) return false;
            return Equals(objAsEventJump);
        }

        public bool Equals(Eventjump other)
        {
            if (other == null) return false;
            return (DiverId.Equals(other.DiverId));
        }

        #endregion

        #endregion
    }
}